package controller

import (
	"admin/contrib/session"
	"admin/model"
	"common/helper"
	"encoding/json"
	"github.com/valyala/fasthttp"
)

type LotteryController struct{}

func (that LotteryController) LotteryConfigBase(ctx *fasthttp.RequestCtx) {
	admin := session.AdminGet(ctx)
	if admin["loginuser"] != "admin" {
		helper.RetData(ctx, false, "权限不足")
		return
	}
	param := model.LotteryParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	if param.Page < 1 {
		param.Page = 1
	}

	if param.PageSize < 10 || param.PageSize > 3000 {
		helper.RetData(ctx, false, "参数错误")
		return
	}

	s, err := model.GetLotteryConfigBase(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

func (that LotteryController) UpdateLotteryConfigBase(ctx *fasthttp.RequestCtx) {
	admin := session.AdminGet(ctx)
	if admin["loginuser"] != "admin" {
		helper.RetData(ctx, false, "权限不足")
		return
	}
	param := model.LotteryConfigBase{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	model.InsertLog("奖池配置更新", ctx)
	err = model.UpdateLotteryConfigBase(param, 0)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}

func (that LotteryController) DeleteLotteryConfigBase(ctx *fasthttp.RequestCtx) {
	admin := session.AdminGet(ctx)
	if admin["loginuser"] != "admin" {
		helper.RetData(ctx, false, "权限不足")
		return
	}
	param := model.LotteryConfigBase{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	model.InsertLog("奖池配置删除", ctx)
	err = model.UpdateLotteryConfigBase(param, 1)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}

func (that LotteryController) LotteryIssueBase(ctx *fasthttp.RequestCtx) {
	admin := session.AdminGet(ctx)
	if admin["loginuser"] != "admin" {
		helper.RetData(ctx, false, "权限不足")
		return
	}
	param := model.LotteryParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	if param.Page < 1 {
		param.Page = 1
	}

	if param.PageSize < 10 || param.PageSize > 3000 {
		helper.RetData(ctx, false, "参数错误")
		return
	}

	s, err := model.GetLotteryIssueBase(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

func (that LotteryController) UpdateLotteryIssueBase(ctx *fasthttp.RequestCtx) {
	admin := session.AdminGet(ctx)
	if admin["loginuser"] != "admin" {
		helper.RetData(ctx, false, "权限不足")
		return
	}
	param := model.LotteryIssueBase{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	model.InsertLog("奖池期号更新", ctx)
	err = model.UpdateLotteryIssueBase(param, 0)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}

func (that LotteryController) DeleteLotteryIssueBase(ctx *fasthttp.RequestCtx) {
	admin := session.AdminGet(ctx)
	if admin["loginuser"] != "admin" {
		helper.RetData(ctx, false, "权限不足")
		return
	}
	param := model.LotteryIssueBase{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	model.InsertLog("奖池期号删除", ctx)
	err = model.UpdateLotteryIssueBase(param, 1)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}

func (that LotteryController) LotteryParticipantRecord(ctx *fasthttp.RequestCtx) {
	param := model.LotteryParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	if param.Page < 1 {
		param.Page = 1
	}

	if param.PageSize < 10 || param.PageSize > 3000 {
		helper.RetData(ctx, false, "参数错误")
		return
	}

	s, err := model.GetLotteryParticipantRecord(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

func (that LotteryController) LotteryPrizeBase(ctx *fasthttp.RequestCtx) {
	admin := session.AdminGet(ctx)
	if admin["loginuser"] != "admin" {
		helper.RetData(ctx, false, "权限不足")
		return
	}
	param := model.LotteryParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	if param.Page < 1 {
		param.Page = 1
	}

	if param.PageSize < 10 || param.PageSize > 3000 {
		helper.RetData(ctx, false, "参数错误")
		return
	}

	s, err := model.GetLotteryPrizeBase(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

func (that LotteryController) UpdateLotteryPrizeBase(ctx *fasthttp.RequestCtx) {
	admin := session.AdminGet(ctx)
	if admin["loginuser"] != "admin" {
		helper.RetData(ctx, false, "权限不足")
		return
	}
	param := model.LotteryPrizeBase{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	model.InsertLog("中奖规则更新", ctx)
	err = model.UpdateLotteryPrizeBase(param, 0)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}

func (that LotteryController) DeleteLotteryPrizeBase(ctx *fasthttp.RequestCtx) {
	admin := session.AdminGet(ctx)
	if admin["loginuser"] != "admin" {
		helper.RetData(ctx, false, "权限不足")
		return
	}
	param := model.LotteryPrizeBase{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	model.InsertLog("中奖规则删除", ctx)
	err = model.UpdateLotteryPrizeBase(param, 1)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}

func (that LotteryController) LotteryWinnerRecord(ctx *fasthttp.RequestCtx) {
	param := model.LotteryParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	if param.Page < 1 {
		param.Page = 1
	}

	if param.PageSize < 10 || param.PageSize > 3000 {
		helper.RetData(ctx, false, "参数错误")
		return
	}

	s, err := model.GetLotteryWinnerRecord(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

func (that LotteryController) LotteryPrizeRule(ctx *fasthttp.RequestCtx) {
	admin := session.AdminGet(ctx)
	if admin["loginuser"] != "admin" {
		helper.RetData(ctx, false, "权限不足")
		return
	}
	param := model.LotteryParam{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	if param.Page < 1 {
		param.Page = 1
	}

	if param.PageSize < 10 || param.PageSize > 3000 {
		helper.RetData(ctx, false, "参数错误")
		return
	}

	s, err := model.GetLotteryPrizeRule(param, ctx)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, s)
}

func (that LotteryController) UpdateLotteryPrizeRule(ctx *fasthttp.RequestCtx) {
	admin := session.AdminGet(ctx)
	if admin["loginuser"] != "admin" {
		helper.RetData(ctx, false, "权限不足")
		return
	}
	param := model.LotteryPrizeRule{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {

		helper.RetData(ctx, false, "参数错误")
		return
	}
	model.InsertLog("中奖规则更新", ctx)
	err = model.UpdateLotteryPrizeRule(param, 0)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}

func (that LotteryController) DeleteLotteryPrizeRule(ctx *fasthttp.RequestCtx) {
	admin := session.AdminGet(ctx)
	if admin["loginuser"] != "admin" {
		helper.RetData(ctx, false, "权限不足")
		return
	}
	param := model.LotteryPrizeRule{}
	err := json.Unmarshal(ctx.PostBody(), &param)
	if err != nil {
		helper.RetData(ctx, false, "参数错误")
		return
	}
	model.InsertLog("中奖规则删除", ctx)
	err = model.UpdateLotteryPrizeRule(param, 1)
	if err != nil {
		helper.RetData(ctx, false, err.Error())
		return
	}
	helper.RetData(ctx, true, "成功")
}
